//
//  MJStatusRetweetedView.swift
//  新浪微博
//
//  Created by MaJi on 16/4/13.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class MJStatusRetweetedView: UIView {
//记录底部约束
    var bottomRet: Constraint?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var statusViewModel:MJststusOriganalVIewModel? {
        didSet{
            contentText.text = statusViewModel?.status?.retweeted_status?.text
//            判断转发微博中是否有图片
            self.bottomRet?.uninstall()
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count>0{
//                重新布局
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomRet = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
//                绑定数据
                pictureView.statusPicture = statusViewModel?.status?.retweeted_status?.pic_urls
//                显示view
                pictureView.hidden = false
            }else{
                //                重新布局
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomRet = make.bottom.equalTo(contentText.snp_bottom).offset(StatusCellMargin).constraint
                })
                //                绑定数据
                pictureView.statusPicture = nil
                //                显示view
                pictureView.hidden = true
            }
            
        }
    }
    
    private func setupUI(){
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(contentText)
        addSubview(pictureView)
//        添加布局
        contentText.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentText.snp_bottom)
            make.left.equalTo(contentText.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
//        转发微博view的高度的自动计算
        self.snp_makeConstraints { (make) -> Void in
            self.bottomRet = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    var contentText = UILabel(text: "转发微博正文", textColor: UIColor.darkGrayColor(), fontSize: 12, margin: StatusCellMargin)
    var pictureView:MJStatuePictureView = MJStatuePictureView()
}
